<?php

namespace app\pay\controller;

use think\Controller;

class Index extends Controller
{
  // 管理员模型
  protected $AdminModel = null;

  protected $PayModel = null;

  public function __construct()
  {
    parent::__construct();

    $this->AdminModel = model('Admin');

    $this->PayModel = model('Pay.Pay');
  }

  // 登陆
  public function login()
  {
    if ($this->request->isPost()) {

      $username = $this->request->param('username', '', 'trim');
      $password = $this->request->param('password', '', 'trim');

      $admin = $this->AdminModel->where(['username' => $username])->find();

      if (!$admin) {
        return json(['code' => 0, 'msg' => '账号不存在', 'data' => null]);
      }

      // 匹配密码
      $password = md5(md5($password) . $admin['salt']);

      if ($password !== $admin['password']) {
        return json(['code' => 0, 'msg' => '密码错误', 'data' => null]);
      }

      if ($admin['status'] !== 'normal') {
        return json(['code' => 0, 'msg' => '账号已被禁用', 'data' => null]);
      }

      $data = [
        'id' => $admin['id'],
        'username' => $admin['username'],
        'nickname' => $admin['nickname'],
        'avatar_cdn' => $admin['avatar_text']
      ];

      return json(['code' => 1, 'msg' => '登录成功', 'data' => $data]);
    }
  }
  // 当客户端监听到收款通知后发起请求到这里获取相关的数据
  public function check()
  {
    if ($this->request->isPost()) {
      $price = $this->request->param('price', 0, 'trim');
      $adminid = $this->request->param('adminid', 0, 'trim');

      $paytime = $this->request->param('paytime', '', 'trim');

      $admin = $this->AdminModel->find($adminid);

      if (!$admin) {
        return json(['code' => 0, 'msg' => '账号不存在', 'data' => null]);
      }
      if ($admin['status'] !== 'normal') {
        return json(['code' => 0, 'msg' => '账号已被禁用', 'data' => null]);
      }

      $pay = $this->PayModel->where(['price' => $price, 'status' => 0])->find();

      if (!$pay) {
        return json(['code' => 0, 'msg' => '查询不到该订单', 'data' => null]);
      }

      $paytime = strtotime($paytime);

      $data = [
        'id' => $pay['id'],
        'status' => 1,
        'paytime' => $paytime
      ];

      $result = $this->PayModel->isUpdate(true)->save($data);

      if ($result === false) {
        return json(['code' => 0, 'msg' => '更新订单状态失败', 'data' => null]);
      }

      // 获取更新后的订单数据
      $UpdatePayData = $this->PayModel->find($pay['id']);

      return json(['code' => 1, 'msg' => '查询成功', 'data' => $UpdatePayData]);
    }
  }


  public function status()
  {
    if ($this->request->isPost()) {
      $payid = $this->request->param('payid', 0, 'trim');

      $pay = $this->PayModel->find($payid);

      if (!$pay) {

        return json(['code' => 0, 'msg' => '支付订单不存在', 'data' => $pay]);
      }

      switch ($pay['status']) {
        case 0:
          return json(['code' => 0, 'msg' => '订单未支付', 'data' => $pay]);
          break;

        case 1:
          return json(['code' => 1, 'msg' => '订单已支付', 'data' => $pay]);

          break;

        case 2:
          return json(['code' => 0, 'msg' => '订单已关闭', 'data' => $pay]);

          break;
      }
    }
  }
}
